    var map = null, service = null, timer = null, gps = null, markers = null, titles = {}, contents = {},infoWindow=null,
        airRidePoints = {},gps_point = {};
        // workers={},
        lonlat_array = {};
    //车辆状态(1 待发车 2 已发车 3 已到达 4 已返程)


    var ride_point_color = ["images/marker_blue.png", "images/marker_yellow.png", "images/marker_red.png"];

    airDispatch.service = {
        init: function (url) {
            console.log("service...init");
            map = airDispatch.map;
            service = this;
            gps = airDispatch.gps;
            markers = airDispatch.markers;
            infoWindow = initInfoWindow();
            //乘车点
            initRidePoint();
            airDispatch.timer_ride_point = setInterval(initRidePoint, 1000 * 5);
            //车辆
            createMarkers(url);
            airDispatch.timer_car = setInterval(updateMarkers, 1000 * 10);
        },
        clearBind:function () {
            map = null, service = null, timer = null, gps = null, markers = null, titles = {}, contents = {}, airRidePoints = {}, lonlat_array = {};
            airDispatch.initServiceFlag = false;
            infoWindow = null;
            for(var m in markers){
                markers[m].setMap(null);
            }
            for(var a in airRidePoints){
                airRidePoints[a].setMap(null);
            }
        }
    }

    var initInfoWindow = function () {
        var infoWindow = new AMap.InfoWindow({
            isCustom: true,
            content: "",
            offset: new AMap.Pixel(0,-15)
        });
        return infoWindow;
    }

    var initRidePoint = function () {
        var url = "getRidePointDtoList.do";
        $.ajax({
                url: url,
                type: "GET",
                async: false,
                success: function (result) {
                    var dtoList = result.data;
                    fillWithDtoData(dtoList);
                    // workers = {};
                    if (!airDispatch.initServiceFlag) {
                        console.log("初始化乘车点");
                        airDispatch.initServiceFlag = true;
                        for (var d in dtoList) {
                            var dto = dtoList[d];
                            addRidePointMarker(dto, getRidePointColorIndex(dto.airRidePoint));
                            // load_workers(dto);
                        }
                    } else {
                        console.log("更新乘车点");
                        for (var d in dtoList) {
                            var dto = dtoList[d];
                            var airRidePoint = dtoList[d].airRidePoint;
                            airRidePoints[airRidePoint.id].extData = airRidePoint;
                            airRidePoints[airRidePoint.id].setIcon(ride_point_color[getRidePointColorIndex(airRidePoint)]);
                            if(!airRidePoints[airRidePoint.id]){
                                addRidePointMarker(dto, getRidePointColorIndex(dto.airRidePoint));
                            }
                            // load_workers(dto);
                        }
                    }
                }
            }
        )
    }
    var load_workers = function (dto) {
        var ridePoint = dto.airRidePoint;
        // 添加工作人员
        if(typeof workers[ridePoint.id]==='undefined'){
            workers[ridePoint.id]={};
        }
        var workerList = dto.workerList;
        for(var w in workerList){
            var worker = workerList[w];
            if(worker.lon>0&&worker.lat>0) {
                var w_marker = new AMap.Marker({
                    position: new AMap.LngLat(worker.lon, worker.lat),
                    offset: new AMap.Pixel(-10, -10),
                    icon: "images/worker_icon.png",
                    zIndex: 200,
                    extData: worker
                });
                workers[ridePoint.id][worker.id] = w_marker;
                w_marker.on("click", function (event) {
                    var extData = event.target.Je.extData;
                    console.log(extData);
                    getWorkerDetailByData(extData);
                });
            }
        }
    }
    var fillWithDtoData = function (dtoList) {
        $("#dtoList_right").empty();
        $("#dtoList_left").empty();
        var newHtml1 = '';
        var newHtml2 = '';
        var waitingSum = 0;
        var tookSum = 0;
        var appointSum = 0;
        for (var d in dtoList) {
            var dto = dtoList[d];
            var airRidePoint = dtoList[d].airRidePoint;
            var per1 = (Math.round(airRidePoint.waitingSum/airRidePoint.serRetention * 10000)/ 100.00>100)?100:(Math.round(airRidePoint.waitingSum/airRidePoint.serRetention * 10000)/ 100.00);
            var per2 = 0;
            if(dto.appointSum>0) {
                per2 = (Math.round(dto.tookSum / dto.appointSum * 10000) / 100.00 > 100) ? 100 : (Math.round(dto.tookSum / dto.appointSum * 10000) / 100.00);
            }else {
                if(dto.tookSum>0){
                    per2 = 100;
                }
            }
            newHtml1 += ' <li class="wait-li">\n' +
                '                                <div class="wait-chart" title="候车超过'+ dto.airRidePoint.serRetention +'人，为严重滞留">\n' +
                '                                    <div class="wait-chart-bar" style="width:'+per1+'%"></div>\n' +
                '                                </div>\n' +
                '                                <p class="show-pointinfo" data-pointid="'+airRidePoint.id+'">'+(dto.airRidePoint.route>0?dto.airRidePoint.route:'')+" "+airRidePoint.name+'</p>\n' +
                '                                <p>\n' +
                '                                    <span>候车<em>'+airRidePoint.waitingSum+'</em>人</span>\n' +
                '                                    <span>当前运力<em>'+dto.takerSum+'</em>座</span>\n' +
                '                                </p>\n' +
                '                            </li>';

            newHtml2 += '<li class="list-box">\n' +
                '                                    <div class="list-title">\n' +
                '                                        <span class="list-title-name show-pointinfo" data-pointid="'+airRidePoint.id+'">'+(dto.airRidePoint.route>0?dto.airRidePoint.route:'')+" "+airRidePoint.name+'</span>\n' +
                '                                        <span class="list-title-data">\n' +
                '                                            <em class="main-color">'+dto.tookSum+'</em> / '+dto.appointSum+'\n' +
                '                                        </span>\n' +
                '                                    </div>\n' +
                '                                    <div class="list-chart">\n' +
                '                                        <div class="list-chart-bar" style="width:'+ per2 + '%;"></div>\n' +
                '                                    </div>\n' +
                '                                </li>';

            waitingSum += airRidePoint.waitingSum;
            tookSum += dto.tookSum;
            appointSum += dto.appointSum;
        }
        $("#tookSum").html(tookSum);
        $("#appointSum").html(appointSum);
        $("#waitingSum").html(waitingSum);
        if(waitingSum == 0){
            //todo 隐藏  $("#dtoList_right")
        }else {
            //todo 展开?   $("#dtoList_right")
        }
        $("#dtoList_right").append(newHtml1);
        $("#dtoList_left").append(newHtml2);
        changeMainRightCss();airRidePoint
    }
    var getRidePointColorIndex = function (airRidePoint) {
        if (airRidePoint.waitingSum < airRidePoint.sliRetention) {
            return 0;
        } else if (airRidePoint.waitingSum < airRidePoint.serRetention) {
            return 1;
        } else {
            return 2;
        }
    }
    var addRidePointMarker = function (dto, n) {
        var ridePoint = dto.airRidePoint;
        if(ridePoint.lon&&ridePoint.lat) {
            var marker = new AMap.Marker({
                position: new AMap.LngLat(ridePoint.lon, ridePoint.lat),
                offset: new AMap.Pixel(-10, -10),
                icon: ride_point_color[n],
                extData: ridePoint,
            });
            map.add(marker);
            airRidePoints[ridePoint.id] = marker;
            marker.on("click", function (event) {
                var extData = event.target.Je.extData;
                console.log(extData);
                getRidePointDetailById(extData.id);
            });
            marker.on("mouseover", function (event) {
                var extData = event.target.Je.extData;
                console.log(extData);
                if(extData) {
                    infoWindow.setPosition([extData.lon,extData.lat]);
                    infoWindow.setContent(ContentSet("point",extData));
                    infoWindow.open(airDispatch.map, [extData.lon,extData.lat]);
                }
            });
            marker.on("mouseout", function (event) {
                airDispatch.map.remove(infoWindow);
            });
        }
    };

    function createMarkers(url) {
        console.log("init...");
        $.ajax({
            type: "GET",
            url: url,
            async:false,
            success: function (data) {
                gps = data.data;
                initMarkers(gps);
                // setTitlesAndContents();
                showMarkers();
                console.log("init...success");
            }
        });

    }



    function updateMarkers() {
        console.log("update....");
        $.ajax({
            type: "GET",
            url: "getSpecialGps.do",
            success: function (data) {
                gps = data.data;
                for (var g in gps) {
                    var cData = gps[g];
                    var id = cData.id;
                    // var pointId = cData.airRidePoint.id;
                    var color = car_color[cData.airRidePoint.route - 1];
                    lonlat_array[id] = new AMap.LngLat(cData.lon, cData.lat);
                    if(markers[id]) {
                        //已存在，更新
                        markers[id].moveTo(lonlat_array[id], cData.vat > 30 ? cData.vat * 3 : 100);
                        markers[id].setExtData(cData);
                        markers[id].setIcon(color);
                        // if (!(current_point_id == "" || cData.airRidePoint.id == current_point_id)) {
                        //     markers[id].setMap(null);
                        // }else {
                        //     markers[id].setMap(map);
                        // }
                    }else {
                        //尚未存在，新增
                        var marker = new AMap.Marker({
                            position: new AMap.LngLat(cData.lon, cData.lat),
                            offset: new AMap.Pixel(-10, -10),
                            icon: color,
                            angle: Number(cData.direction) + 90,
                            extData: cData,
                            autoRotation: true
                        });
                        markers[id] = marker;
                        // if (current_point_id == "" || cData.airRidePoint.id == current_point_id) {
                        //     marker.setMap(map);
                        // }
                    }
                    // extData = markers[id].getExtData();
                    // if(typeof extData !=='undefined') {
                    //     if(typeof extData.airRidePoint !=="undefined"){
                    //     if (pointId !== extData.airRidePoint.id) {
                    //         console.log("乘车点调度");
                    //         markers[id].setExtData(cData);
                    //         markers[id].setIcon(car_color[cData.airRidePoint.route - 1]);
                    //         if (current_point_id != '') {
                    //             if (current_point_id == pointId) {
                    //                 console.log("乘车点调度新增。。。");
                    //                 markers[id].setMap(airDispatch.map);
                    //             } else {
                    //                 console.log("乘车点调度调离。。。");
                    //                 markers[id].setMap(null);
                    //             }
                    //         }
                    //     }
                    //     }
                    // }
                }
                gps_point = {};
                for(var a in gps){
                    gps_point[gps[a].id]=gps[g];
                }
                for(var a in markers){
                    //不参与调度，取消展示
                    if(!gps_point[a]){
                        markers[a].setMap(null);
                        delete markers[a];
                    }
                }
                showMarkers();
            }
        });
    }

    function initMarkers(datas) {
        for (var d in datas) {
            var position = datas[d];
            var color = car_color[position.airRidePoint.route - 1];
            var marker = new AMap.Marker({
                position: new AMap.LngLat(position.lon, position.lat),
                offset: new AMap.Pixel(-10, -10),
                icon: color,
                angle: Number(position.direction) + 90,
                extData: datas[d],
                autoRotation: true
            });
            gps_point[datas[d].id] = datas[d];
            markers[datas[d].id] = marker;
            // map.add(marker);
        }
        for (var m in markers) {
            markers[m].on("click", function (event) {
                var extData = event.target.Je.extData;
                console.log(extData);
                getCarDetailByType(extData.id,1);
                // infoWindow.setContent(createInfoWindow(titles[extData.id], contents[extData.id].join("<br/>")));
                // infoWindow.open(airDispatch.map, markers[extData.id].getPosition());
            });
            markers[m].on("mouseover", function (event) {
                var extData = event.target.Je.extData;
                if(extData) {
                    infoWindow.setPosition([extData.lon,extData.lat]);
                    infoWindow.setContent(ContentSet("car",extData));
                    infoWindow.open(airDispatch.map, [extData.lon,extData.lat]);
                }
            });
            markers[m].on("mouseout", function (event) {
                airDispatch.map.remove(infoWindow);
            });
        }
    }
    
    var ContentSet = function (type,data) {
        if(type == "car"){
            var driverHtml = "";
            var driver = data.driver;
            if(driver){
                driverHtml = '<p><em>司机</em>：'+(driver.name ? driver.name : '')+'</p>'+
                    '<p><em>联系电话</em>：'+ (driver.phone ? driver.phone : '') +'</p>'+
                    '<p><em>对讲机</em>：'+ (driver.interphone ? driver.interphone : '') +'</p>'
            }else {
                driverHtml = '<p><em>司机</em>：</p>'+
                    '<p><em>联系电话</em>：</p>'+
                    '<p><em>对讲机</em>：</p>'
            }
            var content = '<div class="chart-tip">' +
                '<span>'+data.carNumber+'</span>'+
                driverHtml +
                '</div>';
            return content;
        }
        if(type == "point"){
            var content = '<div class="chart-tip">' +
                '<span>' + data.route + '号线 ' + data.name+'</span>'+
                '<p><em>负责人</em>：'+ (data.leader ? data.leader : '') +'</p>'+
                '<p><em>联系电话</em>：'+ (data.phone ? data.phone : '') +'</p>'+
                '<p><em>专线巴士</em>：'+ (data.busSum ? data.busSum : '0') +' 辆</p>'+
                '</div>';
            return content;
        }
    };

    var showMarkers = function () {
        for (var m in markers) {
            markers[m].setMap(null);
        }
        for(var r in airRidePoints){
            airRidePoints[r].setMap(null);
        }
        if (current_point_id == "") {
            for (var m in markers) {
                markers[m].setMap(map);
            }
            for(var r in airRidePoints){
                airRidePoints[r].setMap(map);
            }
        } else {
            for (var m in markers) {
                if (markers[m].Je.extData.airRidePoint.id === Number(current_point_id)) {
                    markers[m].setMap(map);
                }
            }
            airRidePoints[current_point_id].setMap(map);
        }
    };

    window.unonload = function () {
        window.clearInterval(timer);
    }
